﻿using Newtonsoft.Json.Linq;
using System.Text;

namespace MqttSample.Business
{
    /// <summary>
    /// 执行业务处理
    /// </summary>
    public class ExecuteBusinessData : IDisposable
    {
        /// <summary>
        /// 执行业务
        /// </summary>
        /// <param name="option">MqttClientOption</param>
        /// <param name="data">CallbackData</param>
        public async Task ExecuteAsync(MqttClientOption option, CallbackData data)
        {
            try
            {
                //赋励4G
                if (option.Key.Contains("mz009_0099") && data.Topic.Contains("huaianxianlt"))
                {
                    var jsonStr = Encoding.UTF8.GetString(data.Data);
                    JObject model = ConvertToStandardAepObject(jsonStr);
                    //Engine.Resolve<ICtWingServer>().dorecvData(model);
                }
            }
            catch (Exception ex)
            {
                //SysLog.Error(ex, "mqtt业务处理");
            }

            await Task.CompletedTask;
        }

        /// <summary>
        /// disposable
        /// </summary>
        public void Dispose()
        {
        }

        #region private methods
        private JObject ConvertToStandardAepObject(string jsonStr)
        {
            var objData = JObject.Parse(jsonStr);
            var devicedata = objData["devicedata"].Value<JObject>();
            var obj = new JObject();
            obj.Add("messageType", "dataReport");
            obj.Add("protocol", "tcp");
            obj.Add("IMEI", devicedata["imei"].Value<string>());
            obj.Add("timestamp", devicedata["createtime"].Value<long>());
            obj.Add("payload", jsonStr);
            return obj;
        }
        #endregion
    }
}
